var PrintReport = function PrintReport() {


};

// 选择报表
PrintReport.prototype.selectReport = function() {
    var dialogId = "printfSelectDialog";
    //commonLang.tip.selectReport替换'选择报表'
    var select = new CommonDialog(dialogId, "400", "350",
        'platform/im/lantop/basic/ReportManagerController/printfselectreport?dialogId=' + dialogId + "&sysId=" + sysId + "&tableNameIds=" + tableNameIds, commonLang.tip.selectReport, false, true, false, false, true);
    select.show();
};

//删除报表
/**
 * remind:提示框
 * choosereportdelete:请选择一个需要删除的报表
 * confirmdelete确定要删除所选报表吗?
 * deletesuccess删除成功!
 * deletefailure 删除失败!
 * message 消息
 * confirm:确认框
 *
 */
PrintReport.prototype.deleteReport = function() {
    var node;
    if(type == "root"){
        node = $("#reportTreeRoot").tree('getSelected');
    }else{
        node = $("#reportTree").tree('getSelected');
    }
    if (null == node) {
        $.messager.alert(commonLang.tip.remind, commonLang.tip.chooseReportDelete, 'warning');
        return;
    }
    $.messager.confirm(commonLang.tip.confirm, commonLang.tip.confirmDelete, function(r){
        if (r) {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: 'platform/im/lantop/basic/ReportManagerController/deleteReportRelation',
                data: {
                    reportId: node.id,
                    tableName: tableNames
                },
                success: function(result) {
                    var msg = "";
                    if (result.flag == 0) {
                        reLoadTree();
                        reLoadTreeRoot();
                        msg = commonLang.optip.deleteSuccess;
                    } else {
                        msg = commonLang.optip.deleteFailure;
                    }
                    $.messager.show({
                        title: commonLang.tip.message,
                        msg: msg,
                        showType : 'slide',
                        timeout: 2000
                    });
                }
            });
        }
    });
};

// 配置报表
/**
 * remind:提示
 * choosereportconfig:请选择一个报表进行配置！
 * confirm:确认
 * directprint 当前报表已配置过数据源, 是否直接打印?
 * noselect 当前没有选中！
 *
 */
PrintReport.prototype.configReport = function() {
    var _this = this;
    var node;
    if(type == "root"){
        node = $("#reportTreeRoot").tree('getSelected');
    }else{
        node = $("#reportTree").tree('getSelected');
    }
    if (null == node || node.attributes.reportType == '0') {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.chooseReportConfig, 'warning');
        return;
    }
    var resultDataSource = node.attributes.resultDataSource;
    var templateId = node.id;

    var outPutFields = node.attributes.fields; // 取出应输出字段
    outPutFields = encodeURI(encodeURI(outPutFields));
    if (undefined == resultDataSource) {
        // 未配置过数据源, 打开配置界面
        var dialogId = "printfConfigDialog";
        var config = new CommonDialog(dialogId, "900", "450",
            'platform/im/lantop/basic/ReportManagerController/printfreportconfig?dialogId=' + dialogId + "&outPutFields=" + outPutFields + "&templateId=" + templateId + "&tableNameIds=" + tableNameIds + "&sysId=" + sysId + "&conditions=" + conditions, '报表配置', false, true, false, false, true);
        config.show();
        return;
    }

    var flag = true;
    var tableNameArray = tableNames.split(',');
    for (var i = 0; i < tableNameArray.length; i++) {
        if (resultDataSource.indexOf(tableNameArray[i]) == -1) {
            flag = false;
            break;
        }
    }
    $.ajax({
        url: baseHref + 'platform/im/lantop/basic/ReportManagerController/toFindReportSetField',
        dataType: 'json',
        async: false,
        type: 'post',
        data: {
            sysId: sysId,
            templateId: node.id,
            tableNames: tableNames
        },
        success: function(r) {
            if(r.flag == "success"){
                flag = true;
            }else{
                flag = false;
            }
        }
    });

    // 如果 数据库中配置的表名与参数传递的相等则直接进行进行打印
    if (!flag) {
        // 未配置过数据源, 打开配置界面
        var dialogId = "printfConfigDialog";
        var config = new CommonDialog(dialogId, "900", "450",
            'platform/im/lantop/basic/ReportManagerController/printfreportconfig?dialogId=' + dialogId + "&outPutFields=" + outPutFields + "&templateId=" + templateId + "&tableNameIds=" + tableNameIds + "&sysId=" + sysId, '报表配置', false, true, false, false, true);
        config.show();
        //parent.$(".panel-tool-max panel-tool-restore").css({display : 'none'});
    } else {
        // 已配置过，则直接打印, 跳转到finereoprt打印界面，开启一个新的标签
        $.messager.confirm(commonLang.tip.confirm, commonLang.tip.directPrint, function(r) {
            if (r) {
                templateId = node.id;
                // 打印操作
                var reportName = encodeURI(encodeURI(node.attributes.cptFileName));
                var printType = $("input[name=dataType]:checked").val();
                var recordIdss = "";
                if ('all' == printType) {
                    recordIdss = "";
                } else {
                    recordIdss = ids;
                    if ("" == recordIdss) {
                        $.messager.alert(commonLang.tip.alert, commonLang.tip.noSelect, 'warning');
                        return;
                    }
                }
                _this.printComm(templateId, recordIdss, reportName);
                //原来的打印页面展示速度太慢
                //window.open(baseHref + 'platform/im/lantop/basic/ReportManagerController/reportview?templateId=' + node.id + "&reportName=" + reportName + "&isPublic=" + isPublic + "&viewName=" + viewName + "&archiveTableIds=" + archiveTableIds + "&type=2&recordIds=" + recordIdss +"&tableNames=" + tableNames + "&sysId=" + sysId + "&conditions=" + conditions,'Derek',"status=yes,toolbar=yes,menubar=no,location=no'");
            } else {
                // 需要弹出配置报表页面
                var outPutFields = node.attributes.fields; // 取出应输出字段
                outPutFields = encodeURI(encodeURI(outPutFields));
                var templateId = node.id;

                var dialogId = "printfSelectConfigDialog";
                var config = new CommonDialog(dialogId, "900", "450",
                    'platform/im/lantop/basic/ReportManagerController/printfreportconfig?type=1&dialogId=' + dialogId + "&outPutFields=" + outPutFields + "&templateId=" + templateId + "&tableNameIds=" + tableNameIds + "&sysId=" + sysId, '报表配置', false, true, false, false, true);
                config.show();
            }
        });
    }
};

// 打印
/**
 * remind:提示
 * choosereportprint:请选择一个报表进行打印！
 * nodatselected:当前没有选中数据，不能进行仅选中打印！
 */
PrintReport.prototype.printf = function() {
    var node;
    if(type == "root"){
        node = $("#reportTreeRoot").tree('getSelected');
    }else{
        node = $("#reportTree").tree('getSelected');
    }
    if (null == node || node.attributes.reportType == '0') {
        $.messager.alert(commonLang.tip.alert, commonLang.tip.chooseReportPrint, 'warning');
        return;
    }
    var recordIds = "";

    var printType = $("input[name='dataType']:checked").val();
    if (printType == 'all') {
        // 打印全部
        recordIds = "";
        fileIds = "";
    } else if (printType == 'select') {
        if ("" == ids) {
            $.messager.alert(commonLang.tip.alert, commonLang.tip.noDataSelected, 'warning');
            return;
        }
        // 打印选中的
        recordIds = ids;
    }
    var reportName = encodeURI(encodeURI(node.attributes.cptFileName));
    //  这里树种选中的模板都是已经配置过的，可以直接打印
    var flags = false;
    $.ajax({
        url: baseHref + 'platform/im/lantop/basic/ReportManagerController/toFindReportSetField',
        dataType: 'json',
        async: false,
        type: 'post',
        data: {
            sysId: sysId,
            templateId: node.id,
            tableNames: tableNames
        },
        success: function(r) {
            if(r.flag == "success"){
                flags = true;
            }
        }
    });
    if(flags){
        this.printComm(node.id, recordIds, reportName);
    }else{
        //提示 没有配置报表，请先进行配置报表!
        $.messager.alert(commonLang.tip.alert, commonLang.tip.toConfigReport, 'warning');
    }
};

PrintReport.prototype.cancel = function() {
    parent.$("#" + dialogId).dialog('close');
};


// 打印Ajax公共方法
PrintReport.prototype.printComm = function(myTemplateId, myRecordIds, myReportName) {
    $.ajax({
        url: baseHref + 'platform/im/lantop/basic/ReportManagerController/reportview',
        dataType: 'json',
        type: 'post',
        data: {
            sysId: sysId, //
            templateId: myTemplateId,
            conditions: conditions+fileIds, //
            reportName: myReportName,
            type: '2', //
            recordIds: myRecordIds,
            tableNames: tableNames, //
            tableNameIds : tableNameIds,
            isPublic: isPublic, //
            viewName: viewName, //
            archiveTableIds: archiveTableIds, //
            reportViewType : reportViewType,
            nodeId : nodeId,
            sysFlag: sysFlag,
            tableColsStr: tableColsStr
        },
        success: function(r) {
            args = {
                type: r.type,
                sql: r.sql,
                reportName: r.reportName
            };
            var fulls = "left=0,screenX=0,top=0,screenY=0,scrollbars=1,toolbar=yes,menubar=1"; //定义弹出窗口的参数
            if (window.screen) {
                var ah = screen.availHeight - 30;
                var aw = screen.availWidth - 10;
                fulls += ",height=" + ah;
                fulls += ",innerHeight=" + ah;
                fulls += ",width=" + aw;
                fulls += ",innerWidth=" + aw;
                fulls += ",resizable";
            } else {
                fulls += ",resizable"; // 对于不支持screen属性的浏览器，可以手工进行最大化。 manually
            }

            var postAddress = baseHref + "ReportServer";
            var paramNames = [];

            paramNames.push('SQL');
            paramNames.push('reportlet');

            var paramValues = [];
            paramValues.push(args.sql);
            paramValues.push(args.reportName);

            window.open(baseHref + "platform/im/lantop/basic/ReportManagerController/toPrintPageJsp?templateId=" + r.tempId, 'lantop_report_print' + new Date().getTime(), fulls);

        }


    });

};